package ucuserslogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/usercenter/internal/svc"
	"navi-mumbai/app/rpc/usercenter/pb"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
	"time"
)

type QueryInviteListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryInviteListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryInviteListLogic {
	return &QueryInviteListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 查询邀请列表
func (l *QueryInviteListLogic) QueryInviteList(req *pb.QueryInviteListReq) (*pb.QueryInviteListResp, error) {
	defer utils.RecoverPanic()

	// 处理时间
	var err error
	var start, end *time.Time
	if req.StartTime != "" {
		var t time.Time
		loc, _ := time.LoadLocation(dbkey.DateTimeZone)
		t, err = time.ParseInLocation(dbkey.DateTimeCNFormat, req.StartTime, loc)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, "invalid start time")
		}
		start = &t
	}
	if req.EndTime != "" {
		var t time.Time
		loc, _ := time.LoadLocation(dbkey.DateTimeZone)
		t, err = time.ParseInLocation(dbkey.DateTimeCNFormat, req.EndTime, loc)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, "invalid end time")
		}
		end = &t
	}
	// 查询更表数据
	userInvites, err := l.svcCtx.UserInviteModel.Find(l.ctx, 0, req.Uid, start, end, req.PageNo, req.PageSize)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, "UserInviteOrderFlowModel.Find error")
	}
	total, err := l.svcCtx.UserInviteModel.Count(l.ctx, 0, req.Uid, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, "UserInviteOrderFlowModel.Count error")
	}
	//总邀请人数
	inviteTotalPerNum, err := l.svcCtx.UserInviteModel.SumInviteTotalPerCount(l.ctx, req.Uid, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, err
	}
	resp := &pb.QueryInviteListResp{}
	resp.Total = total
	resp.TotalInvite = inviteTotalPerNum
	for _, userInvite := range userInvites {
		// 查询用户名
		userAccount, err := l.svcCtx.UserAccountModel.FindOne(l.ctx, userInvite.Uid)
		if err != nil || userAccount == nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, "UserAccountModel.FindOne error")
		}
		// 查询下级邀请的数量
		downTotal, err := l.svcCtx.UserInviteModel.Count(l.ctx, 0, userInvite.Uid, start, end)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, "UserInviteOrderFlowModel.Count error")
		}
		invite := &pb.Invite{
			UserName:   userAccount.AuthKey,
			Count:      downTotal,
			CreateTime: userInvite.CreateTime.Format(dbkey.DateTimeCNFormat),
			UpdateTime: userInvite.UpdateTime.Format(dbkey.DateTimeCNFormat),
		}
		resp.List = append(resp.List, invite)
	}
	return resp, nil
}
